#include "board.h"

uint32_t user_tick = 0;
void TMR4_GLOBAL_IRQHandler(void)
{
    if(tmr_flag_get(TMR4, TMR_OVF_FLAG) != RESET)
    {
      /* add user code... */
      tmr_flag_clear(TMR4, TMR_OVF_FLAG);
      user_tick++;
      
    }

}
void mainTimerInit(void)
{
    uint32_t timer_pre = 0;
    
    /* enable tmr1 clock */
    crm_periph_clock_enable(CRM_TMR4_PERIPH_CLOCK, TRUE);

    /* tmr1 configuration */
    /* time base configuration */
    /* systemclock/192/1000 = 1000hz */
    timer_pre = system_core_clock/1000000; 
    
    tmr_base_init(TMR4, timer_pre, 1000 - 1);// 1ms per tick
    tmr_cnt_dir_set(TMR4, TMR_COUNT_UP);

    /* overflow interrupt enable */
    tmr_interrupt_enable(TMR4, TMR_OVF_INT, TRUE);

    /* tmr1 overflow interrupt nvic init */
    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
    nvic_irq_enable(TMR4_GLOBAL_IRQn, 0, 0);

    /* enable tmr1 */
    tmr_counter_enable(TMR4, TRUE);

}

